package com.harwkin.nb.camera;

import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.harwkin.nb.camera.model.PicsItem;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.utils.ImageLoaderUtil;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.BaseFragment;
import cn.afactory.tygz.ui.base.utils.HarwkinLogUtils;


/**
 * 大图查看的页面
 */
public class PageBigImageFragment extends BaseFragment {
    @ViewInject(R.id.pin_image)
    private MyPinchZoomImageView mRivImage;
    @ViewInject(R.id.img_load)
    private ImageView mImageLoad;
    private AnimationDrawable mAnimationDrawable;
    private PicsItem mData;
    private DisplayImageOptions mOptions;
    public static final String EXTRA_DATA = "DATA_STRING";
    private Bitmap mbitmap = null;

    public static PageBigImageFragment getPageBigImageFragment(PicsItem picsItem) {
        PageBigImageFragment fragment = new PageBigImageFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(EXTRA_DATA, picsItem);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData() {
        mData = (PicsItem)getArguments().getSerializable(EXTRA_DATA);
    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.view_page_imageview, container, false); // 加载fragment布局
        ViewUtils.inject(this, view);
        init();
        showTitleBar(false);
        return view;
    }

    private boolean isNetPath(String imgPath) {
        return !TextUtils.isEmpty(imgPath)
                && imgPath.toLowerCase().contains("http");
    }

    private void init() {
        mOptions = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(false)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .showImageForEmptyUri(R.drawable.ic_pic_loading_default_big)
                .showImageOnFail(R.drawable.ic_pic_loading_default_big)
                .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
                .imageScaleType(ImageScaleType.EXACTLY).build();
        mImageLoad.setImageDrawable(getResources().getDrawable(R.drawable.ic_loading_bar));
        mAnimationDrawable = (AnimationDrawable) mImageLoad.getDrawable();
        mRivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        if (!TextUtils.isEmpty(mData.image)) {
            if (isNetPath(mData.image)) {// 加载网络图片
                loadImage();
            } else {// 加载本地图片
                mImageLoad.setVisibility(View.GONE);
                mRivImage.setVisibility(View.VISIBLE);
                ImageLoaderUtil.loadLocalImage(mRivImage, mData.image);
            }
        }
    }

    public void loadImage() {
//        if(!mData.image.toLowerCase().endsWith(".jpg")){
//            mData.image = mData.image + ".jpg";
//        }
        HarwkinLogUtils.getInstance().info("Harwkin","Big image url = " + mData.image);
        ImageLoader.getInstance().loadImage(
                mData.image,
                mOptions, new ImageLoadingListener() {

                    @Override
                    public void onLoadingStarted(String imageUri,
                                                 View view) {
                        mImageLoad.setVisibility(View.VISIBLE);
                        mRivImage.setVisibility(View.GONE);
                        mAnimationDrawable.start();
                    }

                    @Override
                    public void onLoadingFailed(String imageUri,
                                                View view, FailReason failReason) {
                        mImageLoad.setVisibility(View.GONE);
                        mRivImage.setVisibility(View.VISIBLE);
                        mRivImage
                                .setImageResource(R.drawable.ic_pic_loading_default_small);
                        mAnimationDrawable.stop();
                    }

                    @Override
                    public void onLoadingComplete(String imageUri,
                                                  View view, Bitmap loadedImage) {
                        showMore(loadedImage);
                        mImageLoad.setVisibility(View.GONE);
                        mRivImage.setVisibility(View.VISIBLE);
                        mRivImage.setImageBitmap(loadedImage);
                        mAnimationDrawable.stop();
                    }

                    @Override
                    public void onLoadingCancelled(String imageUri,
                                                   View view) {
                    }
                });
    }

    private void showMore(Bitmap bitmap) {
        mbitmap = bitmap;
    }

    /**
     * 保存文件
     *
     * @param bm
     * @param fileName
     * @throws IOException
     */
    public void saveFile(Bitmap bm, String fileName, String saveFile) throws IOException {
        File file = new File(saveFile);
        if (!file.exists()) {
            file.mkdir();
        }
        File save = new File(saveFile + fileName);
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(save));
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();

    }
}
